<?php

// +----------------------------------------------------------------------
// | H1CMS © OpenSource CMS
// +----------------------------------------------------------------------
// | Copyright (c) 2014-2016 http://www.h1cms.com All rights reserved.
// | Copyright (c) 2014-2016 嘉兴领格信息技术有限公司，并保留所有权利。
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Allen <allen@lg4.cn>
// +----------------------------------------------------------------------

require '../autoload.php';

use h1cms\model\Module;
use Lg\Component\Categories;
use Lg\Component\Paginator;
use Lg\Http\Input;
use Lg\View\View;

// +----------------------------------------------------------------------
// | 模块名称: 内容管理
// +----------------------------------------------------------------------
//初始化
function initialize() {
    screen()->addBreadcrumb(__('插件列表'), url_for('/plugins/plugins.php'), '', false);
}

//文章列表
function indexAction() {
    $paginator = new Paginator(Input::get('page', 1), Input::get('limit', 20), Input::query());
    $filter['offset'] = $paginator->getOffset();
    $filter['pagesize'] = $paginator->getLimit();
    $filter['order_by'] = 'sort_order asc';
    
    $total_rows = Module::count(); //總行數
    $module_list = Module::getAll($filter); //获取內容列表
    $paginator->setTotal($total_rows); //设置分页组件总行数
    screen()->setTitle(__('模块管理'), __('')); //设置页面标题
    return View::make('plugins', [
                'module_list' => $module_list,
                'total_rows' => $total_rows,
                'paginator' => $paginator,
    ]);
}

//添加文章
function addAction() {
    if (Input::isMethod('post')) {
        return create_node();
    }
    //扫描新模块
    screen()->addBreadcrumb(__('添加模块'), '', '', false);
    screen()->setTitle(__('添加模块'), __('模块管理'));
    return View::make('plugins-form', [
    ]);
}

//卸载模块
function uninstallAction() {
    $id = intval(Input::get('id'));
    $module = Module::findOne($id);
    if($module && $module->mod_type == 'system'){
        return redirect('/plugins/plugins.php')->maybe_redirect()->withFlash('error', '核心模块不能卸载');
    }
    Module::delete($id);
    return redirect('/plugins/plugins.php')->maybe_redirect()->withFlash('success', '模块卸载成功');
}

function installAction() {
    $module_name = Input::get('module_name');
    $module_path = ADMIN_PATH . '/' . $module_name;
    if (is_dir($module_path)) {
        $module_spec = $module_path . '/config/spec.php';
        if (is_file($module_spec)) {
            $module_info = include $module_spec;
            $system_modules = lg_get_all_modules();
            if (is_array($module_info) && array_has($module_info, 'module_name') && !in_array($module_info['module_name'], $system_modules)) {
                //save
                db_insert('modules', [
                    'module_name'=>$module_info['module_name'],
                    'displayname'=>$module_info['displayname'],
                    'sort_order'=>$module_info['sort_order'],
                    'avatar'=>$module_info['avatar'],
                    'status'=> 1,
                    'mod_type'=>'user',
                    'added_at'=>time(),
                ]);
                return redirect('/plugins/plugins.php')->maybe_redirect()->withFlash('success', '模块安装成功');
            }
        }
    }
    return redirect('/plugins/plugins.php')->maybe_redirect()->withFlash('error', '模块安装失败');

    
}
